package workers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;

import model.Software;
import workerUIs.SpecificationStepFrame;
import workflow.step.SpecificationStep;

import com.ibm.mq.MQQueueManager;

/**
 * SpecificationStepWorker class.
 * 
 * @author Mark Czotter
 * @author Miklos Szennai
 * 
 */
public class SpecificationStepWorker extends AbstractStepSwingWorker {
	
	private final String desiredName;
	
	public SpecificationStepWorker(SpecificationStepFrame frame, String inputQueueName,
			String outputQueueName) {
		super(frame, inputQueueName, outputQueueName);
		this.desiredName = frame.getSoftwareName();
	}
	
	@Override
	protected Software doInBackground() throws Exception {
		try {
			final MQQueueManager qm = new MQQueueManager(IMQConstants.MQM_NAME);
			doStep(null);
			putMessage(qm, this.outputQueueName, this.software);			
			qm.disconnect();
			return this.software;
		} catch (Exception e) {
			logger.log(Level.SEVERE, "Exception when executing step: " + getClass().getName() + ": " + e.getMessage());
		}
		return null;
	}
	
	@Override
	protected void doStep(Software software) {
		this.software = new SpecificationStep().doStep(desiredName);
	}
	
	public static void main(String[] args)
	{
		Logger logger = Logger.getLogger(SpecificationStepWorker.class.getCanonicalName());
		try
		{
			final SpecificationStepFrame specFrame = new SpecificationStepFrame("Step1: Specification", true, 0, 200);
			specFrame.getNextButton().addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					new SpecificationStepWorker(specFrame, null, IMQConstants.SPEC_OUTPUT_ARCH_INPUT).execute();				
				}
			});
		}
		catch (Exception e)
		{
			logger.log(Level.SEVERE, "Error during initialization: ", e);
		}
	}

}
